package com.bazinga.contact.controller;


import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

import com.bazinga.contact.model.UserDetails;
import com.bazinga.contact.util.HibernateUtil;


public class UserDetailsManager extends HibernateUtil {

	public UserDetails add(UserDetails userDetails) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(userDetails);
		session.getTransaction().commit();
		return userDetails;
	}
	public UserDetails delete(Long id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		UserDetails userDetails = (UserDetails) session.load(UserDetails.class, id);
		if(null != userDetails) {
			session.delete(userDetails);
		}
		session.getTransaction().commit();
		return userDetails;
	}

	@SuppressWarnings("unchecked")
	public List<UserDetails> list() {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<UserDetails> userDetails = null;
		try {
			
			userDetails = (List<UserDetails>)session.createQuery("from UserDetails").list();
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return userDetails;
	}
}
